package com.vastcom.sbjwt.security.auth;

import com.vastcom.sbjwt.security.TokenHelper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class TokenAuthenticationFilter extends OncePerRequestFilter {

   private final Log logger= LogFactory.getLog(this.getClass());
   private TokenHelper tokenHelper;
   private UserDetailsService userDetailsService;

    public TokenAuthenticationFilter(TokenHelper tokenHelper, UserDetailsService userDetailsService) {
        this.tokenHelper = tokenHelper;
        this.userDetailsService = userDetailsService;
    }
//可以在该方法中，进行登录方式设置
    @Override
    protected void doFilterInternal(HttpServletRequest httpServletRequest,
                                    HttpServletResponse httpServletResponse,
                                    FilterChain filterChain)
            throws ServletException, IOException {

        String username;
        String authToken=tokenHelper.getToken(httpServletRequest);
        if(authToken!=null){
            username=tokenHelper.getUsernameFromToken(authToken);

            if(username!=null){
                UserDetails userDetails=userDetailsService.loadUserByUsername(username);
                if(tokenHelper.validateToken(authToken,userDetails)){
                    TokenBasedAuthentication  authentication=new TokenBasedAuthentication(userDetails);
               authentication.setToken(authToken);
                    SecurityContextHolder.getContext().setAuthentication(authentication);
                }
            }

        }
filterChain.doFilter(httpServletRequest,httpServletResponse);
    }
}
